package CreativePattern.PrototypePattern.entity;


import CreativePattern.PrototypePattern.Interfase.Prototype;

// 具体原型类
public class User implements Prototype {
    private String name;
    private int age;
    private String sex;

    private Contact contact;

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public User(String name, int age, String sex,Contact contact) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.contact=contact;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", contact=" + contact +
                '}';
    }

    // 实现克隆方法
    @Override
    public User clone() {
        try {
            //直接克隆是虚引用，属性是对象是复制地址
//            return (User) super.clone();

            //实引用，使用new实现；也可以按照序列化实现
            User user = (User) super.clone();
            user.setContact(new Contact(this.contact.getAddress(),this.contact.getPhone()));
            return user;
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException("Cloning not supported", e);
        }
    }

    // Getter 和 Setter 方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
